package org.example.imageservice.controller;



import org.example.imageservice.Command.CommandProcessor;
import org.example.imageservice.Command.DeleteImageCommand;
import org.example.imageservice.Command.GetImagesCommand;
import org.example.imageservice.Command.UploadFileCommand;
import org.example.imageservice.service.BgimageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;


@CrossOrigin(origins = "*")
@RestController
public class BackgroundImageController {
    private final BgimageService bgimageService;

    @Autowired
    public BackgroundImageController(BgimageService bgimageService) {
        this.bgimageService = bgimageService;

    }
    @GetMapping("/getbi1")
    public List<String> getImages(@RequestParam(name = "param1", required = true) String param1,
                                  @RequestParam(name = "param2", required = true) String param2) throws IOException {

        GetImagesCommand command = new GetImagesCommand(bgimageService, param1, param2);
        CommandProcessor.processCommand(command);

        return command.getImages();
    }
    @PostMapping("/lo11")
    public ResponseEntity<?> uploadFile(@RequestParam("file") MultipartFile uploadfile,
                                        @RequestParam(name = "param2", required = true) String param2) throws IOException {
        UploadFileCommand command = new UploadFileCommand(bgimageService, param2, uploadfile);
        CommandProcessor.processCommand(command);
        return command.getResponse();
    }
    @PostMapping("/delete-image")
    public ResponseEntity<String> deleteImage(@RequestBody String imageUrl) throws IOException {
        DeleteImageCommand command = new DeleteImageCommand(bgimageService, imageUrl);
        CommandProcessor.processCommand(command);
        return command.getResponse();
    }
}